<?php
namespace lib;

class Encrypt
{

    //私钥
    private static $_privateKey = '-----BEGIN RSA PRIVATE KEY-----
MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAvyCB70wbFopCPKvm
td/AQLsAywozXzE9gvuhC4PQ4HsI9KtWAhb+/OESZUjf4aH4hXkI9N2kzIJiXS9v
jx+yLwIDAQABAkB75mH1NBwXV5eq5zFP0/Y4wEG6XimBW1Vp9R+obFJbQqc/Ps7Q
TIYZcXsd9bRPbKj19UF4nS9kPHM9TtkkadKBAiEA8pOHxU2ewWIwA4FQI5hXEffb
n90WkAxld+/j5/6sI+8CIQDJtB4vGcoAGDVlckfWs7Y9Q8HSK0dceKZNyRI/N/oV
wQIgNHDgcS8Omw/3AUr7YDaW3DORL0ZEkm+boAokYwsTe9ECIQDHJWEP3ASHJ1/A
855eKz1pK7b4DOb+M9juv/GWHWx7gQIgF5Or3hZZGdoc014WPSGG/bP083S3KmGm
v+Pdiy0bbww=
-----END RSA PRIVATE KEY-----';

    /**
     * RSA数据解密
     * @param string $encryptString 解密内容
     * @return string 返回解密数据
     */
    public static function rsaDecrypt($decryptString = '')
    {
        $decrypted = '';
        $privateKey = self::$_privateKey;
        openssl_private_decrypt(base64_decode($decryptString), $decrypted, $privateKey);
        return $decrypted;
    }

    /**
     * AES数据加密
     * @param $content 加密数据内容
     * @param $key 加密秘钥
     * @return string
     */
    public static function aesEncrypt($content, $key)
    {
        $encrypted_data = openssl_encrypt($content, 'aes-128-cbc', substr($key, 0, 16), OPENSSL_RAW_DATA, substr($key, 0, 16));
        return base64_encode($encrypted_data);
    }

    /**
     * AES数据解密
     * @param $content 解密数据
     * @param $key 加密秘钥
     * @return string
     */
    public static function aesDecrypt($content, $key)
    {
        $decrypted = openssl_decrypt(base64_decode($content), 'aes-128-cbc', substr($key, 0, 16), OPENSSL_RAW_DATA, substr($key, 0, 16));
        return $decrypted;
    }

}